using System;

using AGG.Image;
using AGG.UI;
using AGG.RasterizerScanline;

namespace AGG
{
    public class component_rendering_application : MatchParentBoundsWidget
    {
        private AGG.UI.SliderWidget m_alpha;
        AGG.UI.CheckBoxWidget m_UseBlackBackground;

        public component_rendering_application()
        {
            m_alpha = new AGG.UI.SliderWidget(5, 5, 320 - 5, 10 + 5);
            m_alpha.ValueChanged += new SliderWidget.ValueChangedEventHandler(NeedInvalidate);
            m_UseBlackBackground = new UI.CheckBoxWidget(5, 5 + 18, "Draw Black Background");
            m_UseBlackBackground.CheckedStateChanged += new CheckBoxWidget.CheckedStateChangedEventHandler(NeedInvalidate);
            m_alpha.label("Alpha={0:F0}");
            m_alpha.range(0, 255);
            m_alpha.value(255);
            AddChild(m_alpha);
            AddChild(m_UseBlackBackground);
            m_UseBlackBackground.TextColor = new RGBA_Bytes(127, 127, 127);
        }

        void NeedInvalidate(object sender, EventArgs e)
        {
            Invalidate();
        }

        public override void OnDraw(Graphics2D graphics2D)
        {
            int distBetween = graphics2D.DestImage.GetBytesBetweenPixelsInclusive();
            ImageBuffer pfr = new ImageBuffer();
            pfr.Attach(graphics2D.DestImage, new blender_gray(3), distBetween, 2, 8);
            ImageBuffer pfg = new ImageBuffer();
            pfg.Attach(graphics2D.DestImage, new blender_gray(3), distBetween, 1, 8);
            ImageBuffer pfb = new ImageBuffer();
            pfb.Attach(graphics2D.DestImage, new blender_gray(3), distBetween, 0, 8);

            ImageClippingProxy clippingProxy = new ImageClippingProxy(graphics2D.DestImage);
            ImageClippingProxy clippingProxyRed = new ImageClippingProxy(pfr);
            ImageClippingProxy clippingProxyGreen = new ImageClippingProxy(pfg);
            ImageClippingProxy clippingProxyBlue = new ImageClippingProxy(pfb);
	
	        ScanlineRasterizer ras = new ScanlineRasterizer();
	        ScanlineCachePacked8 sl = new ScanlineCachePacked8();

            clippingProxy.clear(m_UseBlackBackground.Checked ? new RGBA_Doubles(0, 0, 0) : new RGBA_Doubles(1, 1, 1));

            RGBA_Bytes FillColor = m_UseBlackBackground.Checked ? new RGBA_Bytes(255, 255, 255, (int)(m_alpha.value())) : new RGBA_Bytes(0, 0, 0, (int)(m_alpha.value()));

            VertexSource.Ellipse er = new AGG.VertexSource.Ellipse(Width / 2 - 0.87 * 50, Height / 2 - 0.5 * 50, 100, 100, 100);
	        ras.add_path(er);
            agg_renderer_scanline.Default.render_scanlines_aa_solid(clippingProxyRed, ras, sl, FillColor);
	        
	        VertexSource.Ellipse eg = new AGG.VertexSource.Ellipse(Width / 2 + 0.87*50, Height / 2 - 0.5*50, 100, 100, 100);
	        ras.add_path(eg);
            agg_renderer_scanline.Default.render_scanlines_aa_solid(clippingProxyGreen, ras, sl, FillColor);
	        //renderer_scanlines.render_scanlines_aa_solid(ras, sl, rbg, new gray8(0, unsigned(m_alpha.value())));

            VertexSource.Ellipse eb = new AGG.VertexSource.Ellipse(Width / 2, Height / 2 + 50, 100, 100, 100);
	        ras.add_path(eb);
            agg_renderer_scanline.Default.render_scanlines_aa_solid(clippingProxyBlue, ras, sl, FillColor);
	        //renderer_scanlines.render_scanlines_aa_solid(ras, sl, rbb, new gray8(0, unsigned(m_alpha.value())));

            //m_alpha.Render(ras, sl, clippingProxy);
            //m_UseBlackBackground.Render(ras, sl, clippingProxy);
            base.OnDraw(graphics2D);
        }

        public static void StartDemo()
        {
            GuiHalFactory.SetGuiBackend(GuiHalFactory.KnownGuiFactoriesIndexes.WindowsFormsBitmap);
            GuiHalSurface primaryWindow = GuiHalFactory.CreatePrimarySurface(320, 320, GuiHalSurface.CreateFlags.None, GuiHalSurface.PixelFormat.PixelFormatBgr24);

            primaryWindow.Caption = "AGG Example. Component Rendering";

            primaryWindow.AddChild(new component_rendering_application());
            primaryWindow.Run();
        }

        [STAThread]
        public static void Main(string[] args)
        {
        	StartDemo();
        }
    };
}
